package cube.utils.log;

import static cube.utils.BaseUtil.conf;
import cube.utils.TimeUtil;

/**
 * 日志工具
 * 
 * @author abiao
 */
public class Log {

    /**
     * 调试信息
     */
    private boolean debug = conf.getBool("log.debug.show");

    /**
     * 错误信息
     */
    private boolean error = conf.getBool("log.error.show");

    /**
     * 时间信息
     */
    private boolean showTime = conf.getBool("log.time.show");

    /**
     * 时间格式
     */
    private String timeFormat = conf.get("log.time.format");

    /**
     * 输出调试信息
     */
    public void debug(Object msg) {
        if (debug) {
            System.out.println("DEBUG | " + format(msg));
        }
    }

    /**
     * 输出错误信息
     */
    public void error(Object msg) {
        if (error) {
            System.err.println("ERROR | " + format(msg));
        }
    }

    /**
     * 格式化日志信息
     */
    private String format(Object msg) {
        StringBuffer sb = new StringBuffer();
        if (showTime) {
            sb.append(TimeUtil.getNowTime(timeFormat) + " ");
        }
        return sb.append(msg).toString();
    }

    /**
     * 设置是否显示debug信息
     */
    public Log showDebug(boolean debug) {
        this.debug = debug;
        return this;
    }

    /**
     * 设置是否显示error信息
     */
    public Log showError(boolean error) {
        this.error = error;
        return this;
    }

    /**
     * 设置是否显示时间信息
     */
    public Log showTime(boolean showTime) {
        this.showTime = showTime;
        return this;
    }

    /**
     * 设置时间显示格式(yyyy-MM-dd_HH:mm:ss.SSS)
     */
    public Log setTimeFormat(String timeFormat) {
        this.timeFormat = timeFormat;
        return this;
    }
}
